<%-- 
    Document   : jspModel
    Created on : Feb 29, 2012, 4:11:44 AM
    Author     : 章哲原
--%>

<%@page import="com.cmti.commons.web.AuthenticationFilter"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<%
    pageContext.setAttribute("contextPath", request.getContextPath());
    pageContext.setAttribute("loginUser", session.getAttribute(AuthenticationFilter.AUTH_INFO));
%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">
        <title>JSP Model Page</title>
        <link rel="stylesheet" href="${contextPath}/resources/css/easyui/easyui.css"/>
        <link rel="stylesheet" href="${contextPath}/resources/ueditor/themes/default/ueditor.css"//>
        <link rel="stylesheet" href="${contextPath}/resources/css/style.css"/>
        <link rel="stylesheet" href="${contextPath}/resources/css/icon.css"/>
        <script type="text/javascript" src="${contextPath}/resources/js/jquery.js"></script>
        <script type="text/javascript" src="${contextPath}/resources/js/jquery.form.js"></script>
        
        
        <script type="text/javascript" src="${contextPath}/resources/js/jquery.easyui.min.js"></script>
        <script type="text/javascript" src="${contextPath}/resources/js/datagrid-detailview.js"></script>
        <script type="text/javascript" src="${contextPath}/resources/js/easyui-lang-zh_CN.js"></script>
        <script type="text/javascript" src="${contextPath}/resources/js/mask.js"></script>
        <script type="text/javascript" src="${contextPath}/resources/js/js.js"></script>
        <script type="text/javascript" >
            var oldNode ,nowNode;
            var $cList ;
            $(document).ready(function(){
                
                $('#ratioList').treegrid({
                    title:'风险控制比率配置',
                    iconCls:'icon-tedit', //图标  
                    width:470,
                    height:500,
                    nowrap: true,
                    rownumbers: true,
                    animate:true,
                    collapsible:true,
                    singleSelect:true,
                    idField:'id',
                    treeField:'rcDesc',
                    url:'${contextPath}/config/risk/list?cloudId='+${loginUser.cloudId},
                    loadFilter:function(data){return data.risk},
                    columns:[[
                            {field:'rcDesc',title:'风控名称',width:320},
                            {field:'weight',title:'风控比率',width:100,editor:{type:'numberbox',options:{max:1,precision:2}},styler:function(value,rowData){
                                    if(rowData.pId ==0)
                                        return "background:#e3f2e1";
                                    else
                                        return "background:#fffddd";
                                }}
                        ]],
                    toolbar:[{
                            id:'editBt',
                            text:'修改风控比率',
                            iconCls:'icon-edit',
                            handler:function(){
                                var node = $('#ratioList').treegrid('getSelected');
                                nowNode = node;
                                if (node){
                                    $('#ratioList').treegrid('beginEdit',node.id);
                                    if(oldNode == node){
                                        oldNode = null;
                                    }
                                    if(oldNode != null){
                                        $('#ratioList').treegrid('endEdit',oldNode.id);
                                    }
                                    oldNode = node;
                                    $(".datagrid-editable-input:first").select();
                                }
                            }
                        },{
                            id:'cancelBt',
                            text:'取消',
                            iconCls:'icon-undo',
                            handler:function(){
                                if(oldNode != null){
                                    $('#ratioList').treegrid('cancelEdit',oldNode.id);
                                }
                            }
                        },{
                            text:'刷新',
                            iconCls:'icon-reload',
                            handler:function(){
                                $('#ratioList').treegrid('reload');
                            }
                        },{
                            id:'saveBt',
                            text:'保存修改',
                            iconCls:'icon-ok',
                            handler:function(){
                                $.messager.confirm('友情提示', '您确定要保存吗？', function(r){
                                    if(r){
                                        if(nowNode){
                                            $('#ratioList').treegrid('endEdit',nowNode.id);
                                        }
                                
                                        var data = $('#ratioList').treegrid('getData');
                                        var s = '';
                                        var sv = '';
                                        var total = 0;
                                        var sumflag=true;
                                        $.each(data,function(i,v){
                                            total += v.weight*1000;
                                            var sum = 0;
                                            if (s != ''){
                                                s += ',';
                                            } 
                                            if (sv != ''){
                                                sv += ',';
                                            }
                                            s+=v.id;
                                            sv += v.weight;
                                            $.each(v.children,function(ii,vv){
                                                sum +=vv.weight*1000;
                                                if (s != ''){
                                                    s += ',';
                                                } 
                                                if (sv != ''){
                                                    sv += ',';
                                                }
                                                s+=vv.id;
                                                sv += vv.weight;
                                            });
                                            if(sum != 1000){
                                                $.messager.alert('友情提示', '风控名称为"'+v.rcDesc+'"的子风控比率总和不等于1，无法保存，请修正！', 'error');
                                                sumflag = false;
                                                return false;
                                            }else{
                                                sumflag = true;
                                            }
                                        });
                                        if(!sumflag){
                                            return false;
                                        }
                                        if(total != 1000){
                                            $.messager.alert('友情提示', '风控比率总和不等于1，无法保存，请修正！', 'error');
                                            return false;
                                        }
                                        $("#addRiskForm").append("<input type='hidden' name='ratId' value='"+s+"' />");
                                        $("#addRiskForm").append("<input type='hidden' name='ratValue' value='"+sv+"' />");
                                        var upOptions = {//表单提交前的配置（jquery-form）
                                            url:"${contextPath}/config/risk/modify",//即action地址
                                            type:"POST",
                                            dataType : "json",
                                            //表单提交成功后执行
                                            success:function(updateData){//提交成功后执行的操作，addData（形参，可任意取名）为返回的json，
                                                if(updateData.ifUpdate){
                                                    $.messager.alert('友情提示','保存成功！','info');
                                                    //将角色Id移除，以免下次添加时重复添加
                                                    $('#addDialog').dialog('close');
                                                    $('#riskList').datagrid('reload');
                                                }else{
                                                    $.messager.alert("友情提示","保存失败，请稍后再试！","error");
                                                }
                                            },
                                            error:new jQueryAjaxErrorHandler()
                                        };
                                        $("#addRiskForm").ajaxSubmit(upOptions);//表单进行提交，带上option参数，提交成功后完成option里的动作
                                        $("#addRiskForm").resetForm();
                                        $("input[name=ratId]").remove();
                                        //移除uId隐藏域
                                        $("input[name=ratValue]").remove();
                                        $("input[name=cloudId]").remove();
                                    }
                                });
                            }
                        }
                        //                        ,'-',{
                        //                            id:'passRisk',
                        //                            text:'配置合格分数',
                        //                            iconCls:'icon-wrench',
                        //                            handler:function(){
                        //                                $.ajax({
                        //                                    url:"${contextPath}/config/risk/score",
                        //                                    success:function(score){
                        //                                        $.each(score.config,function(i,v){
                        //                                            $("#nowPass").text(v.value);
                        //                                        })
                        //                                    },
                        //                                    error:new jQueryAjaxErrorHandler()
                        //                                });
                        //                                $("#passRiskDialog").dialog('open');
                        //                            }
                        //                        }
                    ],
                    onDblClickRow:function(){
                        var node = $('#ratioList').treegrid('getSelected');
                        nowNode = node ;
                        if (node){
                            $('#ratioList').treegrid('beginEdit',node.id);
                            if(oldNode == node){
                                oldNode = null;
                            }
                            if(oldNode != null){
                                $('#ratioList').treegrid('endEdit',oldNode.id);
                            }
                            oldNode = node;
                            $(".datagrid-editable-input:first").select();
                        }
                    },
                    onClickRow:function(){
                        if(oldNode != null){
                            $('#ratioList').treegrid('endEdit',oldNode.id);
                        }
                    }
                });
                
               
                $("#pass").validatebox({
                    required:true
                });
                //                $("#passRiskDialog").dialog({
                //                    title:'配置合格分数',
                //                    iconCls:'icon-page-white-wrench',
                //                    closed:true,
                //                    modal:true,
                //                    shadow:false,
                //                    width:260,
                //                    buttons:[{
                //                            text:'保存分数',
                //                            iconCls:'icon-save',
                //                            handler:function(){
                //                                $.messager.confirm('友情提示', '您确定要修改风险控制合格分数吗？', function(r){
                //                                    if(r){
                //                                        var mdOptions = {
                //                                            url:"${contextPath}/config/risk/passrisk",
                //                                            type:"POST",
                //                                            dataType : "json",
                //                                            beforeSubmit : function(){
                //                                                return $("#passRiskForm").form('validate');
                //                                            },
                //                                            //表单提交成功后执行
                //                                            success:function(updataData){
                //                                                if(updataData.result){
                //                                                    $.messager.alert('友情提示','分数修改成功！','info');
                //                                                    $('#passRiskDialog').dialog('close');
                //                                                }else{
                //                                                    $.messager.alert("友情提示","分数修改失败，请稍后再试！","error");
                //                                                }
                //                                            },
                //                                            error:new jQueryAjaxErrorHandler()
                //                                        };
                //                                        $("#passRiskForm").ajaxSubmit(mdOptions);
                //                                    }
                //                                });
                //                            }
                //                        },{
                //                            text:'取消',
                //                            handler:function(){
                //                                $('#passRiskDialog').dialog('close');
                //                            }
                //                        }],
                //                    onBeforeClose:function(){
                //                        $("#passRiskForm").clearForm();
                //                        $("#passRiskForm input").removeClass("validatebox-invalid");
                //                    }
                //                });
                $cList = $('#configList');
                $cList.datagrid({
                    title:'配置列表', //标题  
                    method:'post',  
                    iconCls:'icon-tedit', //图标  
                    singleSelect:true, //多选  
                    height:360, //高度  
                    width:550, //宽度  
                    striped: true, //奇偶行颜色不同  
                    collapsible:true,//可折叠  
                    url:"${contextPath}/config/risk/data", //数据来源  
                    //                      sortName: 'userNo', //排序的列  
                    //                      sortOrder: 'desc', //倒序  
                    remoteSort: true, //服务器端排序  
                    idField:'id', //主键字段  
                    queryParams:{}, //查询条件  
                    pagination:true, //显示分页  
                    rownumbers:true, //显示行号
                    loadMsg:"正在加载，请稍候...",//远程加载时的提示信息
                    columns:[[
                            {field:'remark',title:'配置名称',width:360,sortable:true,
                                formatter:function(v,r,i){
                                    var content = '';
                                    if(v.length>11){
                                        content = v.substring(0,10)+'...';
                                    }else{
                                        content = v;
                                    }
                                    var tip = '<a class="remarkTip" title="'+v+'" >'+content+'</a>';
                                    return tip;
                                }
                            },
                            {field:'value',title:'配置值',width:140,sortable:true,editor:{type:'numberbox',options:{required:true,min:0,precision:5}}}
                        ]],
                    onClickCell: function(rowIndex, field, value){
                        if($cList.data('editing') && $cList.data('editRow')!=null && $cList.data('editRow') != rowIndex){
                            $cList.datagrid('endEdit',$cList.data('editRow'));
                            $cList.data('editing',false);
                            return false;
                        }
                        //                        $list.datagrid('selectRow',rowIndex);
                        //                        var selected = $list.datagrid('getSelected');
                        if(field == 'value'){
                            $cList.datagrid('beginEdit',rowIndex);
                            $cList.data('editing',true);
                            $cList.data('editRow',rowIndex);
                        }else{
                            if($cList.data('editing')){
                                $cList.datagrid('endEdit',$cList.data('editRow'));
                                $cList.data('editing',false);
                            }
                        }
                        $(".datagrid-editable-input:first").select();
                    },
                    onAfterEdit : function(rowIndex, rowData, changes){
                        if($.isEmptyObject(changes)){
                            return false;
                        }
                        var url = '${contextPath}/config/risk/update';
                        $.ajax({
                            url : url,
                            data : {
                                value : parseFloat(rowData.value),
                                name : rowData.name
                            },
                            cache : false,
                            dataType : "json",
                            type : "POST",
                            success : function(rs) {
                                if(rs.result){
                                    $.messager.show({
                                        title:'友情提示',
                                        msg:rowData.remark+'的系统配置修改成功'
                                    });
                                    $cList.datagrid('load');
                                }
                            }
                        });
                    }
                });
                var p = $cList.datagrid('getPager');
                $(p).pagination({
                    pageSize: 10,//每页显示的记录条数，默认为10 
                    pageList: [5,10,20,30,40,50],//可以设置每页记录条数的列表 
                    beforePageText: '第',//页数文本框前显示的汉字 
                    afterPageText: '页    共 {pages} 页', 
                    displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录'
                });
                $('body').click(function(e){
                    if($cList.data('editRow')!=null){
                        if(!$(e.target).is('.datagrid-body table *')){
                            $cList.datagrid('endEdit',$cList.data('editRow'));
                            $cList.data('editing',false);
                        }
                    }
                });
            });
        </script>
    </head>
    <body>
        <script type="text/javascript">
            $("body").mask({
                maskMsg:"正在初始化，请稍后...",
                timeout:1000,
                opacity:1
            });
        </script>
        <table>
            <tr>
                <td valign ="top">
                    <table id="ratioList"></table>
                    <div class="normalpanel">
                        <form id="addRiskForm"></form>
                    </div>
                </td>
                <td valign ="top">
                    <table id="configList"></table>
                </td>
            </tr>
        </table>

        <!--        <div id="passRiskDialog" class="easyui-dialog">
                    <div class="normalpanel">
                        <form id="passRiskForm">
                            <input type="hidden" name="userId" value="${loginUser.id}"/>
                            <table style="margin-left: auto;margin-right: auto;border-spacing: 3px;">
                                <tr>
                                    <td colspan="2">
                                        <p style="text-indent:24px;">整机销售信息录入结束后计算风险控制分数，如果分数低于配置的合格分数，则自动审核不通过，需要人工对此整机销售信息进行审核。</p>
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan ="2">
                                        <p style="text-indent:24px;">当前合格分数为：<span id="nowPass" class="redFont"></span></p>
                                    </td>
                                </tr>
                                <tr>
                                    <td align="right" width="70px" >
                                        <span class="redFont">*</span>修改分数：
                                    </td>
                                    <td>
                                        <input type="text" name="pass" id="pass" class="normaltext"/>
                                    </td>
                                </tr>
                            </table>
                        </form>
                    </div>
                </div>-->
    </body>
</html>
